package cn.lysy.xiaoruan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import cn.lysy.xiaoruan.R;
import cn.lysy.xiaoruan.javabean.Goodslist;
import cn.lysy.xiaoruan.viewHolder.GoodListViewHolder;

/**
 * Created by Sawyer on 2017/10/16.
 */

public class GoodsListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Goodslist> goodslists=new ArrayList<>();
    private LayoutInflater layoutInflater;
    public GoodsListAdapter(Context context,List<Goodslist> goodslists){
        this.context=context;
        this.goodslists=goodslists;
        layoutInflater=LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=layoutInflater.inflate(R.layout.item_goods_list,parent,false);
        GoodListViewHolder holder=new GoodListViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        GoodListViewHolder goodListViewHolder=(GoodListViewHolder) holder;
        final Goodslist goodslist=(Goodslist) goodslists.get(position);
        String goods_name=goodslist.getGname();
        String goods_money=goodslist.getGprice();
        String goods_pic=goodslist.getGphoto();
        String goodsId=goodslist.getGid();
        if (goods_pic!=null&&goods_pic!=""){
            Picasso.with(context).load(goods_pic).placeholder(R.drawable.downloading)
                    .error(R.drawable.failure).into(goodListViewHolder.goods_pic);
        }
        goodListViewHolder.goods_money.setText(goods_money);
        goodListViewHolder.goods_name.setText(goods_name);
        goodListViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //商品详情
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodslists.size();
    }
}
